<?php
/**
 * Created by : Sandi Rizky Kharisma (sandi.informatics08@gmail.com)
 * This Class Generated for CRUD purpose only
 */
class m_anggota extends CI_Model
{

    function __construct()
    {
        parent::__construct();
    }

    public function get($where = null, $limit = null, $offset = null)
    {
        $this->db->select('anggota.*');
        //$this->db->select('unit.NAMA_UNIT');
        //$this->db->join('unit', 'anggota.ID_UNIT = unit.ID_UNIT');

        if($where != null) $this->db->where($where);
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        return $this->db->get("anggota");
    }

    function get_all($limit, $uri)
    {
        $result = $this->db->get('anggota', $limit, $uri);
        if ($result->num_rows() > 0)
        {
            return $result->result_array();
        } else
        {
            return array();
        }
    }

    function get_one($id)
    {
        $this->db->where('ID_ANGGOTA', $id);
        $result = $this->db->get('anggota');
        if ($result->num_rows() == 1)
        {
            return $result;
        } else
        {
            return array();
        }
    }

    function insert()
    {
        $data = array(
                'ID_UNIT' => $this->input->post('ID_UNIT', TRUE),
                'NO_BARU' => $this->input->post('NO_BARU', TRUE),
                'SN' => $this->input->post('SN', TRUE),
                'GGI' => $this->input->post('GGI', TRUE),
                'JENIS_ANGGOTA' => $this->input->post('JENIS_ANGGOTA', TRUE),
                'NAMA_ANGGOTA' => $this->input->post('NAMA_ANGGOTA', TRUE),
                'NAMA_KOTA' => $this->input->post('NAMA_KOTA', TRUE),
                'NAMA_DIVISI' => $this->input->post('NAMA_DIVISI', TRUE),
                'ALAMAT' => $this->input->post('ALAMAT', TRUE),
                'TELPON1' => $this->input->post('TELPON1', TRUE),
                'TELPON2' => $this->input->post('TELPON2', TRUE),
                'GOLONGAN' => $this->input->post('GOLONGAN', TRUE),
                'MULAI' => $this->input->post('MULAI', TRUE),
                'TGL_LAHIR' => $this->input->post('TGL_LAHIR', TRUE),
                'IURAN_POKOK' => $this->input->post('IURAN_POKOK', TRUE),
                'IURAN_WAJIB' => $this->input->post('IURAN_WAJIB', TRUE),
                'TGL_CATAT' => $this->input->post('TGL_CATAT', TRUE),
        );
        return $this->db->insert('anggota', $data);
    }

    function update($id)
    {
        $data = array(
                'ID_UNIT' => $this->input->post('ID_UNIT', TRUE),
                'NO_BARU' => $this->input->post('NO_BARU', TRUE),
                'SN' => $this->input->post('SN', TRUE),
                'GGI' => $this->input->post('GGI', TRUE),
                'JENIS_ANGGOTA' => $this->input->post('JENIS_ANGGOTA', TRUE),
                'NAMA_ANGGOTA' => $this->input->post('NAMA_ANGGOTA', TRUE),
                'NAMA_KOTA' => $this->input->post('NAMA_KOTA', TRUE),
                'NAMA_DIVISI' => $this->input->post('NAMA_DIVISI', TRUE),
                'ALAMAT' => $this->input->post('ALAMAT', TRUE),
                'TELPON1' => $this->input->post('TELPON1', TRUE),
                'TELPON2' => $this->input->post('TELPON2', TRUE),
                'GOLONGAN' => $this->input->post('GOLONGAN', TRUE),
                'MULAI' => $this->input->post('MULAI', TRUE),
                'TGL_LAHIR' => $this->input->post('TGL_LAHIR', TRUE),
                'IURAN_POKOK' => $this->input->post('IURAN_POKOK', TRUE),
                'IURAN_WAJIB' => $this->input->post('IURAN_WAJIB', TRUE),
                'TGL_CATAT' => $this->input->post('TGL_CATAT', TRUE),
        );
        $this->db->where('ID_ANGGOTA', $id);
        return $this->db->update('anggota', $data);
    }

    function delete($id)
    {
        $this->db->where('ID_ANGGOTA', $id);
        $this->db->delete('anggota');
    }

    function get_simpanan($date){
        $year = date("Y", strtotime($date));
        $month = date("m", strtotime($date));

        $kode_unit = $this->input->post('KODE_UNIT',TRUE);

        $this->db->select('anggota.*');
        $this->db->select('info_simpanan.SIMPANAN_WAJIB');
        $this->db->join('info_simpanan','anggota.KODE_UNIT = info_simpanan.KODE_UNIT');
        //$this->db->where("MONTH(anggota.TGL_IURAN) = $month && YEAR(anggota.TGL_IURAN) = $year");
        $this->db->where("anggota.KODE_UNIT = '$kode_unit'");
        $this->db->where("anggota.STATUS",1);
        return $this->db->get('anggota');
    }

    function generate_realisasi($date,$id_bukti){
        $query = $this->get_simpanan($date);
        $count_done = 0;
        $count_data = $query->num_rows();
        
        $this->db->trans_start();
        foreach($query->result() as $result){
            $data = array(
                'IURAN_WAJIB' => $result->SIMPANAN_WAJIB + $result->IURAN_WAJIB,
                'TGL_IURAN' => ''
            );

            $this->db->where('ID_ANGGOTA',$result->ID_ANGGOTA);
            $this->db->update('anggota',$data);

            if ($this->db->affected_rows() > 0) $count_done++;

            //insert into tabel realisasi
            $data = array(
                    'ID_ANGGOTA' => $result->ID_ANGGOTA,
                    'ID_INFO_USAHA' => 13,
                    'ID_BUKTI_TRANSAKSI' => $id_bukti,
                    'JUMLAH_POTONGAN' => $result->SIMPANAN_WAJIB,
                    'ANGSURAN_KE' => 1,
                    'TGL_REALISASI' => $date
                );

                $this->db->insert('realisasi',$data); 
        }
        $this->db->trans_complete();

        $response = "Data simpanan wajib berhasil direalisasikan sebanyak $count_done dari $count_data data.";
        
        return $response;
        
    }

}
?>
